package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.MotionVideoModel;
import com.app.service.AdminUserService;
import com.app.service.MotionVideoService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 动态视频
 */
@Controller
@RestController
@RequestMapping("/admin")
public class AdminMotionVideoController {
    @Autowired
    private MotionVideoService motionVideoService;
    @Autowired
    private AdminUserService adminUserService;

    //新增和编辑
    @PostMapping("/insertAndUpdateVideo")
    public Result<Object> insertAndUpdateVideo(@RequestBody MotionVideoModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (model.getId() > 0) {
            model.setUpdateTime(CommonHelp.getNowTimestamp());
            motionVideoService.update(model);
        } else {
            model.setCreateTime(CommonHelp.getNowTimestamp());
            if (model.getVideoUrl() != null) {
                model.setType(1);
            } else {
                model.setType(2);
            }
            motionVideoService.insert(model);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteVideo")
    public Result<Object> deleteVideo(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        motionVideoService.deleteById(id);
        return Result.success("操作成功");
    }

    //列表
    @GetMapping("/getVideoList")
    public Result<Object> getVideoList(Integer page, Integer limit, String keyword, Integer type, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(motionVideoService.selectList(page, limit, keyword, type));
    }

    //查看详情
    @GetMapping("/getVideoInfo")
    public Result<Object> getVideoInfo(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(motionVideoService.selectById(id));
    }

    //更新状态
    @GetMapping("/updateVideoStatus")
    public Result<Object> updateVideoStatus(Integer id, Integer status, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        MotionVideoModel model = new MotionVideoModel();
        model.setId(id);
        model.setStatus(status);
        motionVideoService.update(model);
        return Result.success("更新成功");
    }
}
